package com.yjx.controller;

import com.github.pagehelper.PageInfo;
import com.yjx.query.PropertyInfoQuery;
import com.yjx.service.HouseService;
import com.yjx.service.PropertyInfoService;
import com.yjx.service.PropertyTypeService;
import com.yjx.util.JsonObject;
import com.yjx.util.R;
import com.yjx.vo.House;
import com.yjx.vo.PropertyInfo;
import com.yjx.vo.PropertyType;
import com.yjx.vo.Userinfo;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * lzy
 */
@Controller
@RequestMapping("propertyInfo")
public class PropertyInfoController {
    @Resource
    PropertyInfoService propertyInfoService;
    @Resource
    HouseService houseService;
    @Resource
    PropertyTypeService  propertyTypeService;

    //查询
    @RequestMapping("/queryPropertyAll")
    @ResponseBody
    public JsonObject queryPropertyAll(String numbers,Integer status,PropertyInfoQuery propertyInfoQuery){
        propertyInfoQuery.setNumbers(numbers);
        propertyInfoQuery.setStatus(status);
        PageInfo<PropertyInfo>  pageInfo= propertyInfoService.queryPropertyAll(propertyInfoQuery);
        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());
    }


    //删除
    @RequestMapping("/deletePropertyInfoByIds")
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonObject deletePropertyInfo(String ids, HttpServletRequest req){
        HttpSession session = req.getSession();
        Userinfo user = (Userinfo)session.getAttribute("user");
        if(user.getType() == 1){
            List<String> list= Arrays.asList(ids.split(","));
            List<Integer> ids1 = new ArrayList<>();
            for(String id:list){
                Long idLong=new Long(id);
                ids1.add(new Integer(id));
            }
            propertyInfoService.deletePropertyInfo(ids1);
        }
        return new JsonObject(200,"删除成功");
    }

    //收物业费
    @RequestMapping("/initData")
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonObject add(@RequestBody PropertyInfo propertyInfo,HttpServletRequest req){
        HttpSession session = req.getSession();
        Userinfo user = (Userinfo)session.getAttribute("user");
        if(user.getType() == 1){
            List<House> houses = houseService.findList();
            PropertyType type = propertyTypeService.findPropertyTypeById(1);
            for(House  house:houses){
                propertyInfo.setDeleted(1);
                propertyInfo.setStatus(0);
                propertyInfo.setHouseId(house.getId());
                propertyInfo.setTypeId(type.getId());
                propertyInfo.setMoney(house.getArea()*type.getPrice());
                propertyInfoService.add(propertyInfo);
            }
        }
        return new JsonObject(200,"删除成功");
    }


     @PostMapping("update")
     @ResponseBody
     @Transactional(propagation=Propagation.REQUIRED)
    public R update(Integer id){
        int num=propertyInfoService.updateData(id);
        if(num>0){
            return R.ok();
        }
        return R.fail("失败");
    }

}
